//
//  KLTTopBar.m
//  网易新闻头部V2
//
//  Created by qianfeng on 15/9/23.
//  Copyright (c) 2015年 孔令涛. All rights reserved.
//

#import "KLTTopBar.h"

#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define StatusBarHeight 20
#define NavigationHeight 44
#define TabBarHeight 49
#define ToolBarHeight 44
@interface KLTTopBar()<UIScrollViewDelegate>
{
    BOOL isClick;
}
@property (nonatomic,strong) UIScrollView * topScrollView;

@property (nonatomic,strong) UIScrollView * mainScrollView;

@property (nonatomic,copy) NSMutableArray * btnArray;

@property(nonatomic) NSInteger numberOfPage;
@end

@implementation KLTTopBar
+(id)TopBar
{
    return [[self alloc]init];
}

-(void)willMoveToSuperview:(UIView *)newSuperview
{
    self.frame = CGRectMake(0, 0, ScreenWidth ,ScreenHeight);
}


- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        _btnArray  = [[NSMutableArray alloc]init];
        _viewArray = [[NSMutableArray alloc]init];
    }
    return self;
}

-(void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    _numberOfPage = _titleArray.count;
    [self creatTopScrollView];
}

- (void)setViewArray:(NSArray *)viewArray
{
    _viewArray = viewArray;
    [self creatMainView];
}

//设置当前视图的时候加载视图，如果当前视图没变，不作操作，如果当前视图改变并且是空视图，那么就加载数据和控件
-(void)setCurrentView:(TopBarView *)currentView
{
    if (_currentView == currentView)
        return;
    _currentView = currentView;
    if (_currentView.subviews.count == 0) {
        [_currentView loadDataWith:_currentView.tag];
    }
}

//创建主界面滚动视图
- (void)creatMainView
{
    double height = ScreenHeight - 64;
    _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, height)];
    _mainScrollView.delegate = self;
    _mainScrollView.contentSize = CGSizeMake(ScreenWidth * _numberOfPage, height);
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.backgroundColor = [UIColor whiteColor];
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_mainScrollView];
    
    for (int i = 0 ; i < _viewArray.count ; i++)
    {
        [_mainScrollView addSubview:_viewArray[i]];
    }
}

//创建顶部滚动视图
- (void)creatTopScrollView
{
    _topScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 44)];
    [self addSubview:_topScrollView];
    _topScrollView.backgroundColor = [UIColor whiteColor];
    double btnWidth = 80;
    _topScrollView.contentSize = CGSizeMake(btnWidth*_numberOfPage+ScreenWidth/2-40, 0);
    _topScrollView.showsHorizontalScrollIndicator = NO;
    
    //给滚动视图添加按钮
    for ( int i = 0; i < _numberOfPage; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(btnWidth * i, 0, btnWidth, 44);
        [btn setTitle: _titleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_topScrollView addSubview:btn];
        [_btnArray addObject:btn];
        btn.tag = i;
        if (i == 0) {
//            btn.selected = YES;
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
            [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        }
        [btn addTarget: self action:@selector(dealBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}
- (void)dealBtn:(UIButton *)btn
{
    if (btn.selected)
        return;
    for (UIButton * btn  in _btnArray) {
        btn.selected = NO;
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
//    btn.selected = YES;
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    //保证第一个第二个顶部按钮在固定的位置
    
    if (btn.tag <= 1) {
        [_topScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    else
    {
        [_topScrollView setContentOffset:CGPointMake(80 * btn.tag-ScreenWidth/2+40, 0) animated:YES];
    }
  
    
    [_mainScrollView setContentOffset:CGPointMake(ScreenWidth * btn.tag, 0) animated:YES];
    
    self.currentView = _viewArray[btn.tag];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    scrollView = _mainScrollView;
    double offsetX = scrollView.contentOffset.x;
    NSInteger tag = offsetX/ScreenWidth;
    [self dealBtn:_btnArray[tag]];
    self.currentView = _viewArray[tag];
}
//设置当前页面的同时加载当前页面的内容，如果之前加载就不再重新加载

//清理内存的方法
-(void)cleanMemory
{
    
    //保留当前显示页面的内容，清除其他页面的内容
    for (UIView * view in _viewArray) {
        if (_currentView != view) {
            for (UIView * subview in view
                 .subviews) {
                [subview removeFromSuperview];
            }
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
